package com.szhuang.mobilesafe.service;

import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.graphics.PixelFormat;
import android.os.IBinder;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;
import android.widget.Toast;

import com.szhuang.mobilesafe.R;
import com.szhuang.mobilesafe.db.dao.NumberAddressQueryUtils;

/**
 * Created by szhuang on 2015/3/29.
 */
public class AddressService extends Service {

    private TelephonyManager telephonyManager;
    private MyListenerPhone listenerPhone;

    private View view;
    private WindowManager windowManager;

    private OutCallReceiver receiver;
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        init();

    }

    public void init(){
        telephonyManager= (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
        listenerPhone=new MyListenerPhone();
        telephonyManager.listen(listenerPhone,PhoneStateListener.LISTEN_CALL_STATE);

        receiver=new OutCallReceiver();
        IntentFilter intentFilter=new IntentFilter();
        intentFilter.addAction("android.intent.action.NEW_OUTGOING_CALL");
        registerReceiver(receiver,intentFilter);

        windowManager= (WindowManager) getSystemService(WINDOW_SERVICE);
    }


    public void myToast(String address){
        view =View.inflate(this, R.layout.address_show,null);
        TextView textView= (TextView) view.findViewById(R.id.tv_address);

        int[] ids={R.drawable.gray_color,R.drawable.orange_color,R.drawable.deep_orange_color};
        SharedPreferences sp=getSharedPreferences("config",MODE_PRIVATE);
        view.setBackgroundResource(ids[sp.getInt("which",0)]);
        textView.setText(address);

        WindowManager.LayoutParams params=new WindowManager.LayoutParams();
        params.height = WindowManager.LayoutParams.WRAP_CONTENT;
        params.width = WindowManager.LayoutParams.WRAP_CONTENT;
        params.gravity=Gravity.CENTER;

        params.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
                | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE
                | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
        params.format = PixelFormat.TRANSLUCENT;
        params.type = WindowManager.LayoutParams.TYPE_TOAST;
        windowManager.addView(view, params);

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        telephonyManager.listen(listenerPhone,PhoneStateListener.LISTEN_NONE);
        listenerPhone=null;

        unregisterReceiver(receiver);
        receiver=null;
    }




    class MyListenerPhone extends PhoneStateListener{

        @Override
        public void onCallStateChanged(int state, String incomingNumber) {
            super.onCallStateChanged(state, incomingNumber);

            switch (state){
                case TelephonyManager.CALL_STATE_RINGING:
                    String address= NumberAddressQueryUtils.queryNumber(incomingNumber.trim());
//                    Toast.makeText(getApplicationContext(),address,Toast.LENGTH_LONG).show();
                    myToast(address);
                    break;
                case TelephonyManager.CALL_STATE_IDLE:
                    if(view!=null){
                        windowManager.removeView(view);
                    }
                    break;
            }
        }
    }

    class OutCallReceiver extends BroadcastReceiver{

        @Override
        public void onReceive(Context context, Intent intent) {
            String phone=getResultData();
            String address= NumberAddressQueryUtils.queryNumber(phone);
//            Toast.makeText(context,address,Toast.LENGTH_LONG).show();
            myToast(address);
        }
    }
}
